package com.huidao.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

import com.huidao.domain.Food;
import com.huidao.domain.MealsSet;
import com.huidao.domain.Order;
import com.huidao.domain.OrderItem;
import com.huidao.domain.PrizeType;
import com.huidao.domain.VIPCard;
import com.huidao.ui.LoginMenu;
import com.huidao.ui.PrizeMenu;

/**
 * 用户购买商品
 */
public class BuyService {
	private static List<OrderItem> list = new ArrayList<>();
	private static VIPCard vipCard = LoginMenu.getVipCard();
	private static Scanner sc = new Scanner(System.in);
	private static Order order;

	/**
	 * 点餐
	 */
	public void buy() {
		while (true) {
			System.out.println("请输入该菜品的名字：");
			String buyName = sc.next();
			System.out.println("判断该菜品类型，如果是套餐请输入1，不是请输入其他");
			if (sc.nextInt() == 1) {
				if (new MealsSetService().check(buyName) == null) {
					System.out.println("请重新输入");
					buy();
				}
				MealsSet mealset = new MealsSetService().check(buyName);
				System.out.println("输入购买数量");
				int count = sc.nextInt();
				OrderItem item = new OrderItem(Long.toString(System
						.currentTimeMillis()), mealset, count);
				list.add(item);
			} else {
				if (new FoodService().check(buyName) == null) {
					System.out.println("请重新输入");
					buy();
				}
				Food food = new FoodService().check(buyName);
				System.out.println("输入购买数量");
				int count = sc.nextInt();
				OrderItem item = new OrderItem(Long.toString(System
						.currentTimeMillis()), food, count);
				System.out.println("该用户是否为VIP，是请输入1，不是请输入其他");
				if (sc.nextInt() == 1) {
					item.getFood().setPrice(food.getVipPrice());
					System.out.println("会员价："+item.getFood().getPrice());
				}
				list.add(item);
			}
			System.out.println("继续购买请输入1");
			if (sc.nextInt() != 1) {
				break;
			}
		}
		int num = 0;
		OrderItem item[] = new OrderItem[list.size()];
		for (OrderItem orderItem : list) {
			item[num++] = orderItem;
		}
		System.out.println("请选择您要支付订单的方式");
		String payType = sc.next();
		order = new Order(System.currentTimeMillis(), item, payType);
		System.out.println("请选择活动类型：1表示满减，2表示折扣");
		if (sc.nextInt() == 1) {
			new ActivityTypeService().setFullCut();
		} else {
			new ActivityTypeService().setActivityCount();
		}
		new PrizeMenu().show();
		vipCard  = PrizeType.getVipCard();
		if (payType .equals("会员卡支付") ) {
			pay();
		}
		System.out.println("-------------订单信息------------");
		System.out.println(order.getOrderNumber());
		System.out.println("菜品" + "                     " + "价格"
				+ "                       " + "购买数量"
				+ "                         " + "总价");
		for (OrderItem orderItem : item) {
			System.out.println(orderItem.getFood().getName() + "     "
					+ orderItem.getFood().getPrice() + "       "
					+ orderItem.getCount() + "          "
					+ orderItem.getCount() * orderItem.getFood().getPrice());
		}
		System.out.println("订单总价为：" + PrizeType.getTotal());
		System.out.println("订单时间："
				+ new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
						.format((new Date())));
	}

	/** 会员卡余额支付 */
	public void pay() {
		System.out.println("卡上余额" + vipCard.getVipSurplusMoney());
		if (vipCard.getVipSurplusMoney() < PrizeType.getTotal()) {
			System.out.println("余额不足，请充值");
			new VipCardService().Recharge();
		}
		vipCard.setVipSurplusMoney(vipCard.getVipSurplusMoney()
				- PrizeType.getTotal());
		System.out.println("卡上余额：" + vipCard.getVipSurplusMoney());
	}
	/**
	 * @return the list
	 */
	public static List<OrderItem> getList() {
		return list;
	}

	/**
	 * @return the order
	 */
	public static Order getOrder() {
		return order;
	}

	/**
	 * @return the vipCard
	 */
	public static VIPCard getVipCard() {
		return vipCard;
	}

}
